<?php
require 'include.php';
$no = (!empty($_REQUEST['no'])) ? $_REQUEST['no'] : "";
$score = (!empty($_REQUEST['score'])) ? $_REQUEST['score'] : "";
$results = array("3-0","3-1","3-2","2-3","1-3","0-3");
$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
if($action == 'presences'){
	if($user_id = check_cookie()){
		$q = "select id from membre_matchs where membre_id = $user_id and no_match = '$no'";
		$r = ExecRequete($q);
		if($s = LigneSuivante($r)){
			if($s->id){
				$q = "delete from membre_matchs where id = $s->id";
				echo "-1";				
			}
		}
		else{
			$q = "insert into membre_matchs (membre_id,no_match) values($user_id,'$no')";
			echo 1;
		}
		ExecRequete($q);
	}
}
elseif($no){
	$match = new match($no);
	if($score){
		if($user_id = check_cookie()){
			$user = new membre($user_id);
			if((in_array($match->equipe,$user->equipes))||(check_admin())){
				if(in_array($score,$results)){
					$q = "update matchs set resultat = '$score' where no = '$no'";
					echo (ExecRequete($q)) ? (utf8_decode("Résultat enregistré")) : (utf8_decode("une erreur est survenue"));
				}
				else echo "Le résultat n'est pas valide";
			}
			else echo "Permission refusée";
		}
		else echo "Identification échouée";
	}
	else{
		echo "<h3>$match->local - $match->visiteur</h3>\n";
		echo "<div class = 'box'>\n";
		$fields = array("equipe","date","heure","salle","resultat","statistiques","marqueur");
		foreach($fields as $field){
			echo leftCol($field,39);
			if($field == 'equipe') echo rightCol($match->equipe,59);
			elseif($field == 'date') echo rightCol(format_date($match->date),59);
			elseif($field == "heure") echo rightCol(format_heure($match->heure),59);
			elseif($field == 'salle'){
				$salle = $match->get_salle();
				$content = $salle->nom;
				if($salle->adresse) $content .= "<br />".$salle->adresse;
				if($salle->localite) $content .= "<br />".$salle->localite;
				echo rightCol("<a href='http://maps.google.com/maps?q=".(str_replace(" ","+",$salle->adresse))."+".(str_replace(" ","+",$salle->localite))."+switzerland'>".$content."</a>",59);
			}
			elseif($field == 'resultat'){
				if($match->resultat) echo rightCol($match->format_resultat(),59);
				elseif($match->date <= date("Y-m-d")){
					if($user_id = check_cookie()){
						$user = new membre($user_id);
						if((in_array($match->equipe,$user->equipes))||(check_admin())){
							$content = "<select onchange = 'score_match(\"$match->no\",this.options[this.selectedIndex].value)'>";
							foreach($results as $result) $content .= "<option value = '$result'>$result</option>";
							$content .= "</select>";															
						}
						else $content = "<span class = 'fade'>jou&eacute;</span>";
					}
					else $content = "<span class = 'fade'>jou&eacute;</span>";
					echo rightCol($content,59);
				} 
				else echo rightCol("",59);
			}
			elseif($field == 'marqueur'){
				$content = "";
				for($i = 1; $i < 3; $i++){
					if($match->{"marqueur".$i}){
						$equipe = new equipe($match->equipe,"nom");
						$marqueur = new membre($match->{"marqueur".$i});
						$content .= "<a href = 'javascript:nav_back(\"$equipe->id\",\"$equipe->nom\"); membre_detail($marqueur->id)'>$marqueur->prenom $marqueur->nom</a><br />";
					}
				}
				if($content) $content = substr($content,0,-6);
				echo rightCol($content,59);
			}
			elseif($field == 'statistiques'){
				echo rightCol($match->stats(),59);
			}
		}
		if($user_id = check_cookie()){
			$user = new membre($user_id);
			$user_status = "";
			if(in_array($match->equipe,$user->equipes)){
				echo empty_line_two_col();
				echo "<hr />";
				$presences = $match->presences();
				$all_status = array("present" => "pr&eacute;sents",
									"absent" => "absents",
									"licence" => "sans licence");
				foreach($all_status as $stat => $print_stat){
					echo leftCol($print_stat,39);
					$right = "";
					foreach($presences as $joueur => $status){
						if($status == $stat){
							$tab = explode(":",$joueur);
							if($tab[1] == $user_id){
								$user_status = $stat;	
								$right .=  "<span id = '".$stat."_user'><a href = 'javascript: membre_detail($tab[1])'>$tab[0]</a>&nbsp;;</span> ";
								
							} 
							else $right .=  "<a href = 'javascript: membre_detail($tab[1])'>$tab[0]</a>&nbsp;; ";
						} 
					}
					if($stat != 'licence' && $user_status != $stat) $right .= "<span id = '".$stat."_user' style = 'display: none'><a href = 'javascript: membre_detail($user_id)'>$user->prenom</a>&nbsp;; </span>";
					if($right) $right = substr($right,0,-3)	;
					echo rightCol($right,59);
				}
			}
		}
		echo "<br class = 'clear' />\n";
		echo "</div>\n";
		if($user_id = check_cookie() && $user_status != 'licence'){
			if($user_status == 'present'){
				echo "<p class = 'center' id = 'button_user_status_present'><button type = 'button' onclick =  'update_presences(\"$no\")'>je serais absent</button></p>";					
				echo "<p class = 'center' id = 'button_user_status_absent' style = 'display: none'><button type = 'button' onclick =  'update_presences(\"$no\")'>je serais pr&eacute;sent</button></p>";					
			}
			elseif($user_status == 'absent'){
				echo "<p class = 'center' id = 'button_user_status_present' style= 'display: none'><button type = 'button' onclick = 'update_presences(\"$no\")'>je serais absent</button></p>";					
				echo "<p class = 'center' id = 'button_user_status_absent'><button type = 'button' onclick =  'update_presences(\"$no\")'>je serais pr&eacute;sent</button></p>";					
			}
		} 
	}		
}
?>